package cc.idoone.controller;

import cc.idoone.model.ValidateModel;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;
import java.security.NoSuchAlgorithmException;

@Controller
@RequestMapping(value = "/validate")
public class ValidateController {

    @RequestMapping(value = "/test", method = {RequestMethod.GET})
    public String test(Model model) {

        if (!model.containsAttribute("contentModel")) {
            model.addAttribute("contentModel", new ValidateModel());
        }
        return "validatetest";
    }

    @RequestMapping(value = "/test", method = {RequestMethod.POST})
    public String test(Model model,
                       @Valid @ModelAttribute("contentModel") ValidateModel validateModel,
                       BindingResult result) throws NoSuchAlgorithmException {

        //如果有验证错误 返回到form页面
        if (result.hasErrors())
            return test(model);
        return "validatesuccess";
    }

    // 需要异常捕获
    @RequestMapping(value = "/test2", method = {RequestMethod.POST})
    public String test2(@Valid @ModelAttribute("contentModel") ValidateModel validateModel) throws NoSuchAlgorithmException {
        return "validatesuccess";
    }

}